package com.example.restatr.Atcivity.Main;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import android.content.Intent;
import android.os.Bundle;
import android.os.Looper;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.example.restatr.Atcivity.Main.Fragment.UserFragment;
import com.example.restatr.R;
import com.example.restatr.Tools;
import com.google.android.material.button.MaterialButton;

import org.json.JSONException;
import org.json.JSONObject;

public class SetPswAt extends AppCompatActivity {
    private Toolbar tb;
    private TextView barTitle;
    private EditText search;
    private EditText OldPsw;
    private EditText NewPsw;
    private MaterialButton button;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate( savedInstanceState );
        setContentView( R.layout.activity_set_psw_at );


        tb = findViewById( R.id.toolbar);
        barTitle = findViewById( R.id.bar_Title );
        search = findViewById( R.id.search );
        OldPsw = findViewById( R.id.OldPsw );
        NewPsw = findViewById( R.id.NewPsw );
        button = findViewById( R.id.button );

        Tools.SetBarTitle( tb,barTitle,"修改密码",search,SetPswAt.this,false );

        button.setOnClickListener( new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final String oldPsw = OldPsw.getText().toString();
                final String newPsw = NewPsw.getText().toString();
                new Thread( new Runnable() {
                    @Override
                    public void run() {
                        JSONObject jsonObject = new JSONObject();
                        try {
                            jsonObject.put( "newPassword", newPsw );
                            jsonObject.put( "oldPassword", oldPsw );
                            String put = Tools.Put( "/prod-api/api/common/user/resetPwd", UserFragment.token, jsonObject.toString() );
                            if (put != null) {
                                JSONObject jieXi = new JSONObject( put );
                                Looper.prepare();
                                Toast.makeText( SetPswAt.this, jieXi.getString( "msg" ), Toast.LENGTH_SHORT ).show();
                                if (jieXi.getInt( "code" ) == 200) {
                                    UserFragment.code = 0;
                                    finish();
                                    startActivity( new Intent( SetPswAt.this, LoginAt.class ) );
                                }
                                Looper.loop();
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                } ).start();
            }
        } );
    }
}